package com.aier.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import com.aier.common.common.Constant;

public class CompareImage {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // 获取数据库中所有的图片名称（dbList）
        String url = "http://192.168.18.31:8080/aier360/zxadminjs/caipu_searchAllImage.shtml";
        String str = "";
        try {
            URL u = new URL( url );
            URLConnection uc = u.openConnection();
            HttpURLConnection httpUC = (HttpURLConnection)uc;
            httpUC.setDoOutput( true );
            httpUC.setUseCaches(false);
              
            httpUC.setRequestProperty("Content-type", "application/x-java-serialized-object");
            httpUC.setRequestMethod("POST");
              
            httpUC.connect();
            httpUC.getOutputStream();
            int res = httpUC.getResponseCode(); 
            
            if (res == Constant.num_200) {
                InputStream in = httpUC.getInputStream(); 
                int ch; 
                StringBuilder sb2 = new StringBuilder(); 
                while ((ch = in.read()) != -1) { 
                    sb2.append((char) ch); 
                } 
                str = sb2.toString();
            }
            httpUC.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        str = str.substring(str.indexOf("image_str_list")+Constant.num_17);
        str = str.substring(0,str.indexOf("]"));
        
        String[] array = str.split(",");
        List<String> dbList = new ArrayList<>();
        for (int i=0;i<array.length;i++) {
            if (null != array[i]) {
                dbList.add(array[i].substring(1, array[i].length()-1));
            }
        }
        
//        String path = "f:/img.txt";
//        List<String> fileList = writeToDat(path);
        
        // 获取服务器的文件夹中的图片(fileList)
        List<String> fileList = null;
        try {
            SFTPUtils sftpUtil = new SFTPUtils();
            fileList = sftpUtil.runCmd("ls /data/aier360/img/test", "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 比较之后去掉相同的图片，剩下是需要删除的图片（fileList）
        for (int i=0;i<dbList.size();i++) {
            for(int j=0;j<fileList.size();j++) {
                if (fileList.get(j).contains(dbList.get(i))) {
                    fileList.remove(j);
                    break;
                }
            }
        }
        for (int m=0;m<fileList.size();m++) {
            System.out.println(fileList.get(m));
        }
        
    }
    
    public static List<String> writeToDat(String path) {
        File file = new File(path);
        List<String> list = new ArrayList<String>();
        try {
            BufferedReader bw = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = bw.readLine()) != null){
                list.add(line);
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

}
